package net.llz.job;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class MyJobHandler {
    private Logger log = LoggerFactory.getLogger(MyJobHandler.class);

    @XxlJob(value = "demoJobHandler",init = "init",destroy = "destroy")
    public void execute(){

        //获取调度器参数
        String param= XxlJobHelper.getJobParam();
        System.out.println("v2 param="+param);

        log.info("小滴课堂 execute 任务触发成功:"+ LocalDateTime.now());

        XxlJobHelper.log("v2这个是执行日志：param="+param);
        //XxlJobHelper.handleFail("自定义错误，任务执行失败");
        XxlJobHelper.handleSuccess("任务执行成功");
        //return ReturnT.SUCCESS;
    }


    @XxlJob(value="shardingJobHandler")
    public void shardingJobHandler(){
        //当前执行器编号0,1,2,3
        int shardIndex=XxlJobHelper.getShardIndex();
        //当前执行器总数(4)
        int shardTotal = XxlJobHelper.getShardTotal();

        log.info("分片总数:{},当前分片数{}",shardTotal,shardIndex);

        List<Integer> allUserIds = getAllUserIds();

        allUserIds.forEach(obj->{
           if(obj%shardTotal==shardIndex){
               log.info("第{}片，命中分片开始处理用户id={}",shardIndex,obj);
           }
        });

    }
    private List<Integer> getAllUserIds(){

        List<Integer> ids = new ArrayList<>();
        for(int i=0;i<100;i++){
            ids.add(i);
        }

        return ids;
    }


    private void  init(){
        log.info("init 方法调用成功");
    }

    private void destroy(){
        log.info("destroy 方法调用成功");
    }
}
